package com.library.dao;

import com.library.bean.Admin;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 借阅管理员dao层
 * author  weizhihao
 * Created by 2020-03-22-14:12
 */
@Repository
public class AdminBorrowDao {

    private final static String NAMESPACE = "com.library.dao.AdminBorrowDao.";
    @Resource
    private SqlSessionTemplate sqlSessionTemplate;

    public int addAdmainBorrow(final Admin admin){
        return sqlSessionTemplate.insert(NAMESPACE + "addAdmainBorrow", admin);
    }

    public int deleteAdminBorrow(final long admin_id){
        return sqlSessionTemplate.delete(NAMESPACE + "deleteAdminBorrow", admin_id);
    }

    public int updateAdminBorrowMessage(final Admin admin){
        return sqlSessionTemplate.update(NAMESPACE + "updateAdminBorrowMessage", admin);
    }

    public int updatePowerToAdmin(final long admin_id){
        return sqlSessionTemplate.update(NAMESPACE + "updatePowerToAdmin", admin_id);
    }

    public int updatePowerToAdminBorrow(final long admin_id){
        return sqlSessionTemplate.update(NAMESPACE + "updatePowerToAdminBorrow", admin_id);
    }

    public ArrayList<Admin> findAllAdmainBorrow(){
        List<Admin> result = sqlSessionTemplate.selectList(NAMESPACE + "findAllAdmainBorrow");
        return (ArrayList<Admin>) result;
    }

    public Admin findAdmainBorrowById(final long admin_id){
        return sqlSessionTemplate.selectOne(NAMESPACE + "findAdmainBorrowById", admin_id);
    }

    public int resetAdminBorrowPassword(final long admin_id, final String password){
        Map<String, Object> map = new HashMap<>();
        map.put("admin_id", admin_id);
        map.put("password", password);
        return sqlSessionTemplate.update(NAMESPACE + "resetAdminBorrowPassword", map);
    }

    public String getAdminBorrowPassword(final long admin_id){
        return sqlSessionTemplate.selectOne(NAMESPACE + "getAdminBorrowPassword", admin_id);
    }

}
